package com.jsoup.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jsoup.bean.Stayword;
import com.jsoup.service.StaywordService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/stayWordManage")
public class StayWordManageController {

    @Resource
    private StaywordService staywordService;

    @RequestMapping("/searchStayWord")
    @RequiresRoles("admin")
    public String searchStayWord(@RequestParam(value = "pn", defaultValue = "1") String pn,
                                 @RequestParam(value = "content") String content,
                                 Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        content = content.trim().replaceAll(" ", "");
        Stayword stayword = new Stayword();
        stayword.setStaywordSubject(content);
        PageHelper.startPage(pageNo, 10);
        List<Stayword> staywordList = staywordService.searchStayword(stayword);
        PageInfo<Stayword> pageInfo = new PageInfo<>(staywordList);
        model.addAttribute("pageInfo", pageInfo);
        return "manage-stayword";
    }

    /**
     * 删除留言
     *
     * @param staywordId
     * @return
     */
    @RequestMapping("/deleteStayWord")
    @RequiresRoles("admin")
    @ResponseBody
    public Object deleteStayWord(@RequestParam(value = "staywordId") Integer staywordId) {
        Map<Object, Object> map = new HashMap<>();
        int count = staywordService.deleteByPrimaryKey(staywordId);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 回复留言
     * 1、将回复的信息存入数据库
     * 2、然后调用邮箱发送邮箱信息
     *
     * @param stayword
     * @return
     */
    @RequestMapping("/replyStayWord")
    @RequiresRoles("admin")
    @ResponseBody
    public Object replyStayWord(Stayword stayword) {
        Map<Object, Object> map = new HashMap<>();
        if (stayword.getStaywordId() != null && stayword.getStaywordEmail() != null && stayword.getReplyContent() != null) {
            stayword.setStaywordState(1);
            stayword.setReplyDate(new Timestamp(System.currentTimeMillis()));
            int count = staywordService.updateByPrimaryKeySelective(stayword);
            // todo 发送到邮箱
            if (count > 0) {
                map.put("result", "success");
            }
        }
        return map;
    }

    /**
     * 留言列表
     *
     * @param pn
     * @param model
     * @return
     */
    @RequestMapping("/stayWordList")
    @RequiresRoles("admin")
    public String stayWordList(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 10);
        List<Stayword> articleList = staywordService.selectStayword(new Stayword());
        PageInfo<Stayword> pageInfo = new PageInfo<>(articleList);
        model.addAttribute("pageInfo", pageInfo);
        return "manage-stayword";
    }
}
